package com.lccviolet;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiChaochao
 * @Date 2021/10/30 11:10
 * @Description LRU算法，不依赖LinkedHashMap来实现。参考AQS
 *              map负责查找；双向链表复杂添加、删除
 */
public class LRUCacheDemo02 {

    // 1.构建Node节点，作为数据载体
    class Node<K, V> {
        K key;
        V value;
        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
            this.prev = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }
    }

    // 2.构建双向队列，存放Node节点
    class DoubleLinkedList<K, V> {
        Node<K, V> head; //头结点
        Node<K, V> tail; //尾结点

        // 2.1 队列的初始化
        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();

            head.next = tail;
            tail.prev = head;
        }

        // 2.2 添加到头位置
        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;

        }

        // 2.3 删除节点
        public void removeNode(Node<K, V> node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
            node.prev = null;
            node.next = null;
        }

        // 2.4 获得最后一个节点：让其出队列
        public Node getLast() {
            return tail.prev;
        }

    }

    private int cacheSize; //容量大小
    Map<Integer, Node<Integer, Integer>> map; //查找
    DoubleLinkedList<Integer, Integer> doubleLinkedList; //队列，存储node

    public LRUCacheDemo02(int cacheSize) {
        this.cacheSize = cacheSize;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }

    // 获取元素
    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }

        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node); //从原来队列删除
        doubleLinkedList.addHead(node); //添加到队头

        return node.value;
    }

    // 写元素
    public void put(int key, int value) {
        if (map.containsKey(key)) { //存在，执行更新操作
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);

            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else { //不存在，执行添加操作
            if (map.size() == cacheSize) { //容量满了，删除最久未使用的元素，然后添加新元素
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);
        }
    }

    // TODO 使用节点时，不仅要将队列中该节点移到头位置，而且还要将map中该节点的key的顺序也移动到最前面
    public static void main(String[] args) {
        LRUCacheDemo02 lruCacheDemo02 = new LRUCacheDemo02(3);
        lruCacheDemo02.put(1, 1);
        lruCacheDemo02.put(2, 1);
        lruCacheDemo02.put(3, 1);

        System.out.println(lruCacheDemo02.map.keySet());

        lruCacheDemo02.put(4, 1);
        System.out.println(lruCacheDemo02.map.keySet());

        // "3"又进来一次
        lruCacheDemo02.put(3, 1);
        System.out.println(lruCacheDemo02.map.keySet());
        // 使用“2”
        lruCacheDemo02.get(2);
        System.out.println(lruCacheDemo02.map.keySet());

        lruCacheDemo02.put(5, 1);
        System.out.println(lruCacheDemo02.map.keySet());
    }
}
